package com.tjh.gulimall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.tjh.gulimall.order.entity.OrderEntity;
import com.tjh.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjh.common.utils.PageUtils;
import com.tjh.common.utils.Query;

import com.tjh.gulimall.order.dao.OrderItemDao;
import com.tjh.gulimall.order.entity.OrderItemEntity;
import com.tjh.gulimall.order.service.OrderItemService;

/**
 * day113：
 *      @RabbitListener 类+方法上（监听哪些队列即可）
 *      @RabbitHandler  标在方法上（重载区分不同的消息）
 * Create by koala on 2021-01-24 21:50:38
 */
@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * day113：queues:声明需要监听的所有队列
     */
    /*@RabbitListener(queues = {"hello-java-queue"})
    public void recieveMessage(Message message, OrderReturnReasonEntity content) throws InterruptedException {
        System.out.println("接收到消息……内容：" + content + "");
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到消息……内容：" + message + "===>类型：" + message.getClass());
        System.out.println("接收到消息……内容：" + message + "===>内容：" + content);
        //Thread.sleep(3000);//阻塞，说明RabbitMQ是一条一条接收的，一条处理完后才能接收下一条
        System.out.println("消息处理完成 ===>" + content.getName());
    }*/

    /**
     * day113：@RabbitHandler 标在方法上（重载区分不同的消息）
     */
    @RabbitHandler
    public void recieveMessage2(Message message, OrderReturnReasonEntity content,
                                Channel channel) throws InterruptedException {
        System.out.println("recieveMessage2 接收到消息……内容：" + content + "");

        /**
         * day115开始：消费端确认
         * 测试：
         *      1、浏览器访问：localhost:9000/sendMq
         *      2、查看控制台输出
         *      3、访问 http://127.0.0.1:15672/#/ 观察MQ中Queues的消息数量、状态变化
         */
        //deliveryTag：在channel内会按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag ===> " + deliveryTag);
        //签收货物，非批量模式
        try {
            if(deliveryTag%2 == 0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物 ===> " + deliveryTag);
            }else {
                //退货
                /**
                 * 第三个参数：
                 *      false：表示将该货物丢弃
                 *      true：表示将该货物发回RabbitMQ服务器，让RabbitMQ服务器重新入队
                 */
                channel.basicNack(deliveryTag,false,true);
                System.out.println("未签收货物 ===> " + deliveryTag);
            }
        }catch (Exception e){
            //网络中断
        }
        /**
         * day115结束
         */
    }

    /**
     * day113：@RabbitHandler 标在方法上（重载区分不同的消息）
     */
    @RabbitHandler
    public void recieveMessage3(Message message, OrderEntity content) throws InterruptedException {
        System.out.println("recieveMessage3 接收到消息……内容：" + content + "");
    }

}